# src/crud/sys_objects_field.py
from typing import List, Optional
from sqlalchemy.orm import Session
from src.models import sys_objects_field as models
from src.schemas import sys_objects_field as schemas
from src.schemas import request_model
from src.core import orm_curd
from src.schemas.login_manager import AuthManager


# ---------------------- 单条新增 ----------------------
def create_object_field(db: Session,
                        auth: AuthManager,
                        obj_field: schemas.SysObjectsFieldCreate) -> dict:
    """
    新增单条对象字段
    """
    return orm_curd.Insert(
        db,
        auth,
        models.SysObjectsField,
        schemas.SysObjectsField,
        "对象字段",
        [obj_field.dict()],
        ["api_id", "object_id", "field"],  # 业务唯一键
        skip_duplicates=True
    )


# ---------------------- 批量新增 ----------------------
def create_object_fields(db: Session,
                         auth: AuthManager,
                         obj_fields: List[schemas.SysObjectsFieldCreate]) -> dict:
    """
    批量新增对象字段
    """
    return orm_curd.Insert(
        db,
        auth,
        models.SysObjectsField,
        schemas.SysObjectsField,
        "对象字段",
        obj_fields,
        ["api_id", "object_id", "field"],
        skip_duplicates=True
    )


# ---------------------- 分页查询 ----------------------
def get_object_fields(db: Session,
                      auth: AuthManager,
                      page: int = 0,
                      page_size: int = 100,
                      filter_model: Optional[request_model.QueryModel] = None,
                      id: Optional[str] = None) -> dict:
    """
    分页查询对象字段列表
    """
    return orm_curd.query_with_page(
        db,
        auth,
        models.SysObjectsField,
        schemas.SysObjectsField,
        id=id,
        filter_model=filter_model,
        page=page,
        page_size=page_size
    )


# ---------------------- 单条更新 ----------------------
def update_object_field(db: Session,
                        auth: AuthManager,
                        field_id: str,
                        obj_field: schemas.SysObjectsField) -> schemas.SysObjectsField:
    """
    更新单条对象字段
    """
    update_model = request_model.UpdateModel([
        request_model.UpdateItem(
            pk_names=["id"],
            pk_values=[field_id],
            data=obj_field.dict(exclude_unset=True)
        )
    ])
    return orm_curd.update(
        db,
        auth,
        models.SysObjectsField,
        update_model
    )


# ---------------------- 批量更新 ----------------------
def update_object_fields(db: Session,
                         auth: AuthManager,
                         updateModel: request_model.UpdateModel) -> dict:
    """
    批量更新对象字段
    """
    return orm_curd.update(
        db,
        auth,
        models.SysObjectsField,
        updateModel
    )


# ---------------------- 删除 ----------------------
def delete_object_fields(db: Session,
                         auth: AuthManager,
                         deleteModel: request_model.DeleteModel) -> dict:
    """
    删除对象字段（硬删除）
    """
    return orm_curd.delete(
        db=db,
        auth=auth,
        model_cls=models.SysObjectsField,
        delete_obj=deleteModel
    )
